<template>
  <div class="json-parser">
    <!-- 左侧容器 -->
    <div class="left-container">
      <div class="input-container">
        <textarea v-model="jsonData" placeholder="输入 JSON 数据" rows="10"></textarea>
        <div class="button-container">
          <button @click="parseJson">解析 JSON</button>
        </div>
      </div>
    </div>

    <!-- 右侧容器 -->
    <div class="right-container">
      <div class="output-container">
        <div class="variable-input">
          <input type="text" v-model="variableName" placeholder="输入变量名或数组名">
          <button @click="displayVariableResult">显示结果</button>
        </div>
        <textarea v-model="parsedData" placeholder="解析结果" rows="10"></textarea>
      </div>
    </div>
    <div class="introduction">
      <p>
        JSON（JavaScript Object Notation）解析器是一种用于解析和处理 JSON 数据的工具。JSON 是一种轻量级的数据交换格式，常用于将数据从一个系统传输到另一个系统，特别是在 Web 开发中广泛使用。
      </p>
    </div>
  </div>
  
</template>

<script>
export default {
  data() {
    return {
      jsonData: '',
      parsedData: '',
      variableName: '',
    };
  },
  methods: {
    parseJson() {
      try {
        this.parsedData = JSON.stringify(JSON.parse(this.jsonData), null, 2);
      } catch (error) {
        this.parsedData = 'Invalid JSON';
      }
    },
    displayVariableResult() {
      try {
        const parsed = JSON.parse(this.jsonData);
        const variable = parsed[this.variableName];
        if (variable !== undefined) {
          this.parsedData = JSON.stringify(variable, null, 2);
        } else {
          this.parsedData = `Variable "${this.variableName}" not found`;
        }
      } catch (error) {
        this.parsedData = 'Invalid JSON';
      }
    },
  },
};
</script>

<style scoped>

.introduction {
  position: absolute;
  bottom: 10%;
  width: 100%;
  margin: 0%;
  padding: 0px;
  background-color: #f0f0f0;
}

.json-parser {
  position: relative;
  display: flex;
  align-items: stretch;
  padding: 0;
  width: 99%;
  height: 100%;
  bottom: 0;
}

.left-container {
  width: 50%;
  padding: 0;
  margin: 5px;
}

.input-container {
  margin-bottom: 10%;
  width: 90%;
  height: 87%;
  left: 45%;
}

.input-container textarea {
  width: 100%;
  height: 70%;
  padding: 8px;
  font-size: 16px;
  border-radius: 10px;
}

.button-container {
  display: flex;
  margin-top: 10px;
}

.button-container button {
  padding: 8px 16px;
  margin: 0 10px;
  font-size: 16px;
  border-radius: 10px;
  cursor: pointer;
}

.right-container {
  width: 50%;
  padding: 0 10px;
}

.output-container {
  position: relative;
  top: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  width: 99%;
  height: 70%;
}

.variable-input {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.variable-input input {
  flex: 1;
  padding: 8px;
  font-size: 16px;
  border-radius: 10px;
}

.variable-input button {
  padding: 8px 16px;
  margin-left: 10px;
  font-size: 16px;
  border-radius: 10px;
  cursor: pointer;
}

.output-container textarea {
  flex: 1;
  width: 100%;
  margin-top: 10px;
  padding: 8px;
  font-size: 16px;
  border-radius: 10px;
}
</style>
